--
-- Author: shunguo.chen
-- Date: 2016-06-20 12:47:35
--
-- ShareView

local SharePanel = import(".SharePanel")
local ShareMountPanel = import(".ShareMountPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.ShareScene

function ClassRef:init()
	self._ui.over:setVisible(false)
	self._ui.tips:setOpacity(0)

	self._ui.btn_back:addClickEventListener(function()
			executeFunc(self._onCloseView)
		end)
	-- self._ui.btn_share:addClickEventListener(function()
	-- 		executeFunc(self._onOperate, "share", self._data)
	-- 	end)
	self._ui.Button_q:addClickEventListener(function()
			executeFunc(self._onOperate, "shareQQ", self._data)
		end)
	self._ui.Button_w:addClickEventListener(function()
			executeFunc(self._onOperate, "shareWX", self._data)
		end)

	dump(self._data, "ShareView.data")

	self:initPanel()
	self:addTouchNode()
end

function ClassRef:initPanel()
	if self._data.type == 1 then
		self._ui.share:addChild(self._data.param, -1)
		self._ui.PageView:setVisible(false)
	elseif self._data.type == 2 then
		self._mountPanel = ShareMountPanel.create()
		self._mountPanel:setData(self._data.mounts)
		self._ui.share:addChild(self._mountPanel, -1)
		self._ui.PageView:setVisible(false)
		self:performWithDelay(function()
				self:initFgPanel()
				self:setCompStatus(self._data.gotNum, self._data.maxNum)
			end, 0.4)
	elseif self._data.type == 3 then
		self._ui.PageView:setCustomScrollThreshold(1/4 * self._ui.PageView:getContentSize().width)

		self._curPage = nil
		local maxPage = self._data.num
		local bossDatas = self._data.bossDatas
		local compData = self._data.compData
		local i = 0
		local function createPage()
			i = i + 1
			if i <= maxPage then
				local panel = SharePanel.create(Res["ShareBossPanel_" .. i])
				panel:setNewTag(i)
				panel:setData(bossDatas[i])
				panel:setCompData(compData[i], #bossDatas[i])
				if maxPage == self._data.param then
					if i == 1 then
						self._ui.PageView:addPage(panel)
					else
						self._ui.PageView:insertPage(panel, i-2)
					end
				elseif 1 == self._data.param then
					if i == maxPage then
						self._ui.PageView:insertPage(panel, 0)
					else
						self._ui.PageView:addPage(panel)
					end
				else
					self._ui.PageView:addPage(panel)
				end
			end
			if i == maxPage then
				self:initFgPanel()

				local function pageViewEvent(sender, eventType)
		            if eventType == ccui.PageViewEventType.turning then
		                local pageIndex = sender:getCurPageIndex()
		                if self._curPage == pageIndex then return end
		                self._curPage = pageIndex
		                self:switchPage()
		            end
		        end
		        self._ui.PageView:addEventListener(pageViewEvent)
		        self:moveToDiff(self._data.param)

		        self:stopAction(self._action)
		        self._action = nil
			end
		end

		if self._action then
			self:stopAction(self._action)
			self._action = nil
		end
		self._action = self:schedule(createPage, 0.2)
		createPage()
	end
end

function ClassRef:initFgPanel()
	local fg = cc.CSLoader:createNode(Res.ShareFgPanel, true)
	fg:setPosition(display.cx, display.cy)
	fg:findChild("Panel_2"):setContentSize(display.size)
	self._ui.fg:addChild(fg)
	ccui.Helper:doLayout(fg)
	self._fgPanel = fg

	local con = fg:findChild("Panel_2/data")
	con:findChild("num_lv"):setString(UD.player.level)
	con:findChild("zhiye"):setString( UD.player:getClassName() )
	con:findChild("Server"):setString(UD.player.name .. "\n" .. UD.zoneName)

	if self._data.type == 2 then
		con:setPositionY(con:getPositionY() + 100)
		fg:findChild("Panel_2/tlt"):setString(L("collect"))
	elseif self._data.type == 3 then
		fg:findChild("Panel_2/tlt"):setString(L("done"))
		self._fgBtns = {}
		local maxPage = self._data.num
		for i=1,maxPage do
			local btn = fg:findChild("Panel_2/btn_" .. i)
			btn:setVisible(true)
			self._fgBtns[i] = btn
			btn:addEventListener(function()
				btn:setSelected(true)
				self:moveToDiff(i)
			end)
		end
	end

	fg:setOpacity(0)
	local fadeIn = cc.FadeIn:create(1)
	fg:runAction(fadeIn)
	self._ui.tips:runAction(fadeIn:clone())
end

function ClassRef:addTouchNode()
	-- self._touchNode = cc.Node:create()
	-- self:addChild(self._touchNode, 10)

	-- local beganPos
	-- local function beganCB(pos)
	-- 	beganPos = pos
	-- 	return true
	-- end
	-- local function endCB(pos)
	-- 	if beganPos.x == pos.x and beganPos.y == pos.y then
	-- 		self._ui.tips:setVisible(not self._ui.tips:isVisible())
	-- 	end
	-- end
	-- display.nodeTouchOneByOne(self._touchNode, beganCB, endCB)
end

function ClassRef:show()
	self.super.show(self)
end

function ClassRef:moveToDiff(diff)
	local pages = self._ui.PageView:getPages()
	local tarPage
    for i,page in ipairs(pages) do
    	if page:getTag() == diff then
			tarPage = i-1
    		break
    	end
    end

    if self._curPage ~= tarPage then
    	local function moveToPage()
	    	self._curPage = tarPage
	    	self._ui.PageView:moveToPage(tarPage)
	    	self:switchPage()
    	end
    	-- self._ui.PageView:stopAllActions()

    	print("self._curPage", tostring(self._curPage))
    	if self._curPage then
    		-- local page = self._ui.PageView:getPage(self._curPage)
    		-- local tPage = self._ui.PageView:getPage(tarPage)

    		-- local fadeOut = cc.FadeOut:create(0.5)
    		-- local fadeIn = cc.FadeIn:create(0.5)
    		-- local cb = cc.CallFunc:create(moveToPage)
    		-- local action = cc.Sequence:create(fadeOut, cb, fadeIn)
    		-- self._ui.PageView:runAction(action)
    		moveToPage()
    	else
    		moveToPage()
    	end
    end
end

function ClassRef:switchPage()
	print("ShareView.curPage", self._curPage)

    local maxPage = self._data.num
    local pageView = self._ui.PageView

	if self._curPage == 0 then
    	local page = pageView:getPage(maxPage - 1)
    	local new_page = page:clone()
    	pageView:removePage(page)
    	pageView:insertPage(new_page, 0)
    	-- page:retain()
    	-- pageView:removePage(page)
    	-- pageView:insertPage(page, 0)
    	-- page:release()
    	self._ui.PageView:moveToPage(1)
    elseif self._curPage == (maxPage - 1) then
    	local page = pageView:getPage(0)
    	local new_page = page:clone()
    	pageView:removePage(page)
    	pageView:addPage(new_page)
    	-- page:retain()
    	-- pageView:removePage(page)
    	-- pageView:addPage(page)
    	-- page:release()
    	self._ui.PageView:moveToPage(maxPage - 2)
    end
    self._curPage = pageView:getCurPageIndex()

    local page = pageView:getPage(self._curPage)
    local diff = page:getTag()
	for i=1, maxPage do
		self._fgPanel:findChild("Panel_2/lv_" .. i):setVisible(i == diff)
	end
	for i,btn in ipairs(self._fgBtns) do
		btn:setSelected(i == diff)
	end

	self:setCompStatus(page:getCompData())
end

function ClassRef:setCompStatus(gotNum, maxNum)
	self._fgPanel:findChild("Panel_2/num"):setString(gotNum .. "/" .. maxNum)
end

function ClassRef:getCurPage()
	return self._ui.PageView:getPage(self._curPage)
end

function ClassRef:shareVisit()
	if self._data.type == 1 then
		self._data.param:visit()
	elseif self._data.type == 2 then
		self._mountPanel:visit()
		self._fgPanel:visit()
	elseif self._data.type == 3 then
		self:getCurPage():visit()
		self._fgPanel:visit()
	end

	local logo = self._ui.over
  	for i,child in ipairs(logo:getChildren()) do
  		child:visit()
  	end
end

return ClassRef
